<?php
namespace app\api\controller;

use app\common\lib\Show;
use app\common\business\Cart as cartBusi;
/**
 * 购物车类---控制器
*/
class Cart extends AuthBase {
	//添加购物车
	public function add(){
		//判断是否post提交
		if(!$this->request->isPost()){
			//return Show::error();
		}
		$id = input('param.id',0,'intval');
		$num = input('param.num',0,'intval');
		if(!$id || !$num){
			return Show::error([],'参数不合法');
		}

		$res = (new cartBusi())->insertRedis($this->userId,$id,$num);
		if($res === FALSE){
			return Show::error();
		}
		return Show::success(); 
	}

	//获取购物车列表
	public function lists(){
		$ids = input('param.id','','trim');
		$res = (new cartBusi())->lists($this->userId,$ids);
		if($res === FALSE){
			return Show::error();
		}
		return Show::success($res);
	}

	//删除购物车
	public function delete(){
		if(!$this->request->isPost()){
			//return Show::error();
		}
		$id = input('param.id',0,'intval');
		if(!$id){
			return Show::error([],'参数不合法');
		}
		$res = (new cartBusi())->deleteRedis($this->userId,$id);
		if($res === FALSE){
			return Show::error();
		}
		return Show::success($res);
	}

	//更新购物车数量
	public function update(){
		if(!$this->request->isPost()){
			return Show::error();
		}
		$id = input('param.id',0,'intval');
		$num = input('param.num',0,'intval');
		if(!$id || !$num){
			return Show::error([],'参数不合法');
		}
		try{
			$res = (new cartBusi())->updateRedis($this->userId,$id,$num);
		}catch(\think\Exception $e){
			return Show::error([],$e->getMessage());
		}
		if($res === FALSE){
			return Show::error();
		}
		return Show::success($res);
	}
}